package design.patterns.observer;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import java.awt.*;
import java.util.EventListener;

// 事件类，用于封装事件数据
class MessageEvent {
    private final String message;

    public MessageEvent(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

// 观察者类，处理事件
class MessageSubscriber {
    @Subscribe
    public void handleMessageEvent(MessageEvent event) {
        System.out.println("Message received: " + event.getMessage());
    }
}

//静态工具类
public class EventBusCenter {
    public static void main(String[] args) {


        // 创建事件总线
        EventBus eventBus = new EventBus();

        // 创建观察者
        MessageSubscriber subscriber = new MessageSubscriber();

        // 注册观察者到事件总线
        eventBus.register(subscriber);

        // 发布事件
        eventBus.post(new MessageEvent("Hello, world!"));
    }
}